package com.wuyou.securitydemo.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wuyou.sso.common.contant.AuthConstants;
import com.wuyou.securitydemo.filter.AuthorizationFilter;
import com.wuyou.securitydemo.handler.CustomizedFormLoginFailureHandler;
import com.wuyou.securitydemo.handler.CustomizedLogoutSuccessHandler;
import com.wuyou.securitydemo.pwd.CustomizedPasswordSecurityConfig;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter;

/**
 * WebSecurityConfig
 *
 * @Date 2025/3/26 14:45
 * @Author 无忧
 */
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private AuthConfig authConfig;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        super.configure(http);
        // Token为空过滤器
        http.addFilterAfter(authorizationTokenCheckFilter(), WebAsyncManagerIntegrationFilter.class);
        http
                .formLogin()
                .loginPage(AuthConstants.FORM_LOGIN_URI)
                .loginProcessingUrl("/token/form/")
                .failureHandler(customizedFormLoginFailureHandler())
                .disable()
                .logout()
                .logoutSuccessHandler(customizedLogoutSuccessHandler())
                .deleteCookies("JSESSIONID")
                .invalidateHttpSession(true)
                .and()
//                .exceptionHandling(customizedFormLoginFailureHandler())
                .authorizeRequests()
                .antMatchers("/token/**", "/actuator/**")
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .csrf()
                .disable()
                .apply(customizedPasswordSecurityConfig());
//                .apply(mobileSecurityConfigurer())
//                .and()
//                .apply(openidSecurityConfigurer());
    }


    /**
     * 不拦截静态资源
     *
     * @param web
     */
    @Override
    public void configure(WebSecurity web) {
        web.ignoring().antMatchers("/css/**");
    }

    @Bean
    @Override
    @SneakyThrows
    public AuthenticationManager authenticationManagerBean() {
        return super.authenticationManagerBean();
    }


    @Bean
    public CustomizedLogoutSuccessHandler customizedLogoutSuccessHandler(){
        return new CustomizedLogoutSuccessHandler();
    }

    @Bean
    public AuthenticationFailureHandler customizedFormLoginFailureHandler(){
        return new CustomizedFormLoginFailureHandler();
    }
    /**
     * https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released#password-storage-updated
     * Encoded password does not look like BCrypt
     *
     * @return PasswordEncoder
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
//        return new BCryptPasswordEncoder();
    }


//    @Bean
//    public AbstractAuthenticationProcessingFilter customizedPasswordAuthenticationProcessingFilter(){
//        return new CustomizedPasswordAuthenticationProcessingFilter();
//    }

    @Bean
    public CustomizedPasswordSecurityConfig customizedPasswordSecurityConfig(){
        return new CustomizedPasswordSecurityConfig();
    }

    public AuthorizationFilter authorizationTokenCheckFilter() {
        AuthorizationFilter filter = new AuthorizationFilter();
        filter.setIgnoreUrls(authConfig.getIgnoreUrls());
        return filter;
    }

}
